package com.kmust.dream.wanderingdream.donation.service.impl;

import com.kmust.dream.wanderingdream.comm.pojo.User;
import com.kmust.dream.wanderingdream.comm.utils.PageUtils;
import com.kmust.dream.wanderingdream.comm.utils.Query;
import com.kmust.dream.wanderingdream.donation.dao.RecipientUserUnionDao;
import com.kmust.dream.wanderingdream.donation.entity.RecipientUserUnionEntity;
import com.kmust.dream.wanderingdream.donation.service.RecipientUserUnionService;
import com.kmust.dream.wanderingdream.donation.vo.BasicInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.kmust.dream.wanderingdream.donation.dao.BasicInformationDonationDao;
import com.kmust.dream.wanderingdream.donation.entity.BasicInformationDonationEntity;
import com.kmust.dream.wanderingdream.donation.service.BasicInformationDonationService;
import org.springframework.transaction.annotation.Transactional;


@Service("basicInformationDonationService")
public class BasicInformationDonationServiceImpl extends ServiceImpl<BasicInformationDonationDao, BasicInformationDonationEntity> implements BasicInformationDonationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BasicInformationDonationEntity> page = this.page(
                new Query<BasicInformationDonationEntity>().getPage(params),
                new QueryWrapper<BasicInformationDonationEntity>()
        );
        page.setSize(6);//设置每页记录数
        long pages = page.getRecords().size();//得到总记录数
        page.setPages(pages);//设置一共分了几页(传入后自动计算)
        page.setTotal(pages);//设置总记录数
        return new PageUtils(page);
    }
    @Autowired
    BasicInformationDonationDao donationDao;
    @Autowired
    RecipientUserUnionDao recipientUserUnionDao;
    @Override

    @Transactional
    public void saveBasicInfo(BasicInfoVo basicInfoVo,String username) {
        String idNum = basicInfoVo.getIdNumber();
        String accNum = basicInfoVo.getAccountNumber();
        String recipId = idNum.substring(12,18)+accNum.substring(accNum.length()-4);
        RecipientUserUnionEntity recipientUserUnionEntity = new RecipientUserUnionEntity()
                .setUsername(username).setRecipientId(recipId);
        recipientUserUnionDao.insert(recipientUserUnionEntity);

        BasicInformationDonationEntity basicInformationDonationEntity =new BasicInformationDonationEntity()
                .setAccountNumber(basicInfoVo.getAccountNumber())
                .setIdNumber(basicInfoVo.getIdNumber())
                .setBirthday(basicInfoVo.getBirthday())
                .setAge(basicInfoVo.getAge())
                .setCreateTime(new Date())
                .setPoliticalOutlook(basicInfoVo.getPoliticalOutlook())
                .setReceivingAddress(basicInfoVo.getReceivingAddress())
                .setRecipientId(recipId)
                .setSchool(basicInfoVo.getSchool())
                .setLearningSection(basicInfoVo.getLearningSection())
                .setTargetMoney(basicInfoVo.getTargetMoney())
                .setTotalMoney(basicInfoVo.getTotalMoney())
                .setStatus(0)
                .setName(basicInfoVo.getName());
        donationDao.insert(basicInformationDonationEntity);
    }

    @Override
    public User getUserByReciId(String id) {
        User user = donationDao.getUserInfo(id);

        return user;
    }

    @Override
    public List<BasicInformationDonationEntity> getStudentList() {
        QueryWrapper<BasicInformationDonationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<BasicInformationDonationEntity> list = donationDao.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<BasicInformationDonationEntity> getListByUserName(String username) {
        return donationDao.getListByUserName(username);
    }

    @Override
    public List<BasicInformationDonationEntity> getListByType(String type, int curr) {
        int current = (curr-1)*6;
        return donationDao.getListByType(type,current);
    }

    @Override
    public List<BasicInformationDonationEntity> getPage(int curr, int pageSize) {
        return null;
    }

    @Override
    public int getPageSize(String type) {
        List<BasicInformationDonationEntity> list =donationDao.getPage(type);
        int size =list.size();
        return size;
    }
    /**
     * 根据用户名查询对应recipt
     * 根据recipt返回basic
     * */
    @Override
    public List<BasicInformationDonationEntity> getBasicByUserName(String username) {
        List<BasicInformationDonationEntity> list = donationDao.getBasicListByUserName(username);

        return list;
    }


}